#include "widget.h"
#include <stdlib.h>

UIState uistate = {0,0,0,0,0,0,0,0,0,0,1,0};

int regionhit(int x, int y, int w, int h)
{
    if (uistate.mousex < x ||
        uistate.mousey < y ||
        uistate.mousex >= x + w ||
        uistate.mousey >= y + h)
        return 0;
    return 1;
}

void label(int id , int x , int y , int w , int h , int bgColor, int btnStrings[8])
{
  	int btnStrBg;
  	int nI,sx;
  	if(btnStrings == NULL) return;
  	if (uistate.redraw != 1) return;

	sx=x+4;
  	disp_rectangle(x, y, x + w,y + h, bgColor);
  	btnStrBg = bgColor;
  	for(nI =0;nI < 8;nI ++){
  	    if(btnStrings[nI] == 0 ) continue;
  	    disp_common16(sx ,y + 4,btnStrings[nI],btnStrBg);
  	    sx = sx + ((btnStrings[nI] > 4095)?16:8);
  	}
}

// Simple button IMGUI widget
int button(int id, int x, int y,int w, int h, int bgColor, int btnStrings[8],int font)
{
  	int btnStrBg;
  	int nI,push = 0;
    int kb_ret = 0;

  	if (regionhit(x, y, w, h)){
  	  	uistate.hotitem = id;
  	  	if (uistate.activeitem == 0 && uistate.mousedown) uistate.activeitem = id;
  	}

  	if (uistate.kbditem == 0) uistate.kbditem = id;
    if (uistate.redraw == 1) { // rend once
        if (uistate.kbditem == id) 	disp_rectangle(x-3, y-3,x + w + 7,y + h + 9, COLOR_RED);
  	    disp_rectangle(x+4, y+4, x+4+w, y+4+h, COLOR_BLACK); // Render    
        if (uistate.hotitem == id) {
            if (uistate.activeitem == id) { // Button is both 'hot' and 'active'
                disp_rectangle(x+2, y+2, x+2+w, y+2+h, COLOR_GRAY);
                push = 1;
            }
            else { // Button is merely 'hot'
                disp_rectangle(x, y, x+w, x+h, COLOR_GRAY);
            }
            btnStrBg = COLOR_GRAY;
        }
        else  { // widget is not hot, but it may be active    
            disp_rectangle(x, y, x+w, y+h, bgColor);
            btnStrBg = bgColor;
        }
        if(btnStrings != NULL) {
            for(nI =0;nI < 8;nI ++) {
                if(btnStrings[nI] == 0 ) continue;
                if(font==0) disp_common16(2*push + x + 12 + nI*16,2*push + y +4,btnStrings[nI],btnStrBg);
                else disp_common_big(2*push + x + 4 + nI*32,2*push + y+16,btnStrings[nI],btnStrBg);
            }
        }
    }

    if ((uistate.kbditem == id)&&(uistate.keypressed == 1)) { // If we have keyboard focus, we'll need to process the keys
        switch (uistate.keyentered)
        {
            case SDLK_LEFT:
            case SDLK_UP:
                uistate.kbditem = uistate.lastwidget;
                break;
            case SDLK_RIGHT:
            case SDLK_DOWN:
                uistate.kbditem = 0;
                uistate.keyentered = 0;
                break;
            case SDLK_RETURN:
                kb_ret = 1;
                break;
            default: break;
        }
        uistate.keypressed = 0;
        uistate.keyentered = 0;       
    }
  	
    uistate.lastwidget = id;
    // If it is hot and active, but mouse is not down, the user must have clicked.
    return (kb_ret||(uistate.mousedown == 0 && uistate.hotitem == id && uistate.activeitem == id))?1:0;
}

int sclbtn(int id, int x, int y,int w, int h, int bgColor)
{
    int push = 0;
    int kb_ret = 0;

    if (regionhit(x, y, w, h)) { // Check whether the widget should be hot
        uistate.hotitem = id;
        if (uistate.activeitem == 0 && uistate.mousedown) uistate.activeitem = id;
    }
    if (uistate.kbditem == 0) uistate.kbditem = id; // If no widget has keyboard focus, take it 
    
    if(uistate.redraw == 1) {
        if (uistate.kbditem == id)  disp_rectangle(x-2, y-2, x+w+2, y+h+2, COLOR_RED); // If we have keyboard focus, show it
        if (uistate.hotitem == id) {
            if (uistate.activeitem == id) { // Button is both 'hot' and 'active'
                disp_rectangle(x+2, y+2, x+2+w, y+2+h, COLOR_GRAY);
                push = 1;
            }
            else { // Button is merely 'hot'
                disp_rectangle(x, y, x+w, y+h, COLOR_GRAY);
            }
        }
        else { // widget is not hot, but it may be active    
            disp_rectangle(x, y, x+w, y+h, bgColor);
        }
        disp_rectangle(x+11+ push*2, y+3+ push*2, x+11+ push*2 + 2, y+3+ push*2+18, UI_FONT);
        disp_rectangle(x+3+ push*2, y+11+ push*2,x+3+ push*2+ 18, y+11+ push*2+2, UI_FONT);
    }

    if ((uistate.kbditem == id)&&(uistate.keypressed == 1)) { // If we have keyboard focus, we'll need to process the keys
        switch (uistate.keyentered)
        {
            case SDLK_LEFT:
            case SDLK_UP:
                uistate.kbditem = uistate.lastwidget;
                break;
            case SDLK_RIGHT:
            case SDLK_DOWN:
                uistate.kbditem = 0;
                break;
            case SDLK_RETURN:
                kb_ret = 1;
                break;
            default: break;
        }
        uistate.keypressed = 0;
        uistate.keyentered = 0;
    }
    uistate.lastwidget = id;
    // If it is hot and active, but mouse is not down, the user must have clicked.
    return (kb_ret||(uistate.mousedown == 0 && uistate.hotitem == id && uistate.activeitem == id))?1:0;
}

// Simple button IMGUI widget
int valbtn(int id , int x , int y , int w , int h , int bgColor , int btnStrings[8] , int* pvalue)   
{
    int nI,btnStrBg;
    int value = *pvalue;
    int kb_ret = 0;

  	if (regionhit(x, y, w, h)){
  	  	uistate.hotitem = id;
  	  	if (uistate.activeitem == 0 && uistate.mousedown) uistate.activeitem = id;
  	}
  	if (uistate.kbditem == 0) uistate.kbditem = id;
    if(uistate.redraw == 1) {  	 // Render work once only  
        if (uistate.kbditem == id) disp_rectangle(x-3, y-3,x + w + 7,y + h + 9, COLOR_RED);
  	    disp_rectangle(x+4, y+4, x+4+w, y+4+h, COLOR_BLACK);
        if (uistate.hotitem == id) {
            if (uistate.activeitem == id) { // Button is both 'hot' and 'active'
                disp_rectangle(x+2, y+2, x+2+w, y+2+h, COLOR_GRAY);
            }
            else { // Button is merely 'hot'
                disp_rectangle(x, y, x+w, x+h, COLOR_GRAY);
            }
            btnStrBg = COLOR_GRAY;
        }
        else  { // widget is not hot, but it may be active    
            disp_rectangle(x, y, x+w, y+h, bgColor);
            btnStrBg = bgColor;
        }
        if(btnStrings != NULL) {
            for(nI =0;nI < 8;nI ++) {
                if(btnStrings[nI] == 0 ) continue;
                disp_common_big(x + 4 + nI*16,y+4,btnStrings[nI],btnStrBg);
            }
        }
    }

    if ((uistate.kbditem == id)&&(uistate.keypressed == 1)) { // If we have keyboard focus, we'll need to process the keys
        switch (uistate.keyentered) {
            case SDLK_LEFT:
                uistate.kbditem = uistate.lastwidget;
                break;
            case SDLK_UP:
                value++;
                break;          
            case SDLK_RIGHT:
                uistate.kbditem = 0;
                break;
            case SDLK_DOWN:
                if (value > 0) value--;
                break;
            case SDLK_RETURN:
                kb_ret = 1;
                break;
            default: break;
        }
        uistate.keypressed = 0;
        uistate.keyentered = 0;   
    }
    uistate.lastwidget = id;
    *pvalue = value;
    // If it is hot and active, but mouse is not down, the user must have clicked.
    return (kb_ret||(uistate.mousedown == 0 && uistate.hotitem == id && uistate.activeitem == id))?1:0;
}

// Simple scroll bar IMGUI widget
int tactswitch(int id, int x, int y, int* pvalue)
{
    int value = *pvalue;
    int v     = *pvalue;
    int xpos  = value*16; // Calculate mouse cursor's relative y offset 

    if (regionhit(x+ xpos +8, y+4, 16, 16)) { // Check for hotness 
        uistate.hotitem = id;
        if (uistate.activeitem == 0 && uistate.mousedown) uistate.activeitem = id;
    }
    if (uistate.kbditem == 0) uistate.kbditem = id; // If no widget has keyboard focus, take it 
    if(uistate.redraw == 1) {  	 // Render work once only      
        if (uistate.kbditem == id) disp_rectangle(x-2, y-2, x+50, y+26, COLOR_RED);  // If we have keyboard focus, show it 
        disp_rectangle(x, y,x+32+16, y+24,  COLOR_DGRAY); // Render the scrollbar 
        disp_rectangle(x+8+xpos, y+4, x+24+xpos, y+20, (uistate.activeitem == id || uistate.hotitem == id)?COLOR_WHITE:COLOR_LGRAY);
        disp_common16(x+ 24 + 32,y+2,value?4246:4306 ,UI_BG);
    }

    if ((uistate.kbditem == id)&&(uistate.keypressed == 1)) { // If we have keyboard focus, we'll need to process the keys
        switch (uistate.keyentered) {
            case SDLK_UP:
                uistate.kbditem = uistate.lastwidget;
                break;
            case SDLK_LEFT:
                value = 0;
                break;          
            case SDLK_DOWN:
                uistate.kbditem = 0;
                break;
            case SDLK_RIGHT:
                v = 1;
                break;
            case SDLK_RETURN:
                return 1;
            default: break;
        }
        uistate.keypressed = 0;
        uistate.keyentered = 0;   
    }
    uistate.lastwidget = id;

    if ((uistate.activeitem == id)&&regionhit(x+ xpos +4, y+4, 16, 16)) v = (v == 1)?0:1;
    if (v != value) {
        *pvalue = v;
        return 1;
    }
    else return 0;    
}

// Simple scroll bar IMGUI widget
int slider(int id, int x, int y, int* pvalue, int max)
{
    // Calculate mouse cursor's relative y offset
	int value = *pvalue;
    int xpos = ((100 - 16) * value) / max;
    int v = *pvalue;
    int show_value = v;
    int mousepos;

    if (regionhit(x+8, y+8, 100, 16))  { // Check for hotness 
        uistate.hotitem = id;
        if (uistate.activeitem == 0 && uistate.mousedown) uistate.activeitem = id;
    }
    if (uistate.kbditem == 0) uistate.kbditem = id; // If no widget has keyboard focus, take it 
    if(uistate.redraw == 1) {  	 // Render work once only        
        if (uistate.kbditem == id) disp_rectangle(x-2, y-2, x+118, y+34, COLOR_RED); // If we have keyboard focus, show it
        disp_rectangle(x, y,x+100+16, y+32,  COLOR_DGRAY);  // Render the scrollbar
        disp_rectangle(x+8+xpos, y+8, x+24+xpos, y+24,(uistate.activeitem == id || uistate.hotitem == id)?COLOR_WHITE:COLOR_LGRAY);
        show_value = v;
        if(show_value > 99) {
            disp_common16(x+ 20 + 100,y+4,show_value/100 + 1,UI_BG);
            show_value = show_value%100;
            x += 8;
        }
        if(show_value > 9) {
            disp_common16(x+ 20 + 100,y+4,show_value/10 + 1,UI_BG);
            show_value = show_value%10;
            x += 8;
        } 
        disp_common16(x+ 20 + 100,y+4,show_value + 1,UI_BG);
    }

    if ((uistate.kbditem == id)&&(uistate.keypressed == 1)) { // If we have keyboard focus, we'll need to process the keys
        switch (uistate.keyentered) {
            case SDLK_UP:
                uistate.kbditem = uistate.lastwidget;
                break;
            case SDLK_LEFT:
                if (v > 0)  v--;
                break;          
            case SDLK_DOWN:
                uistate.kbditem = 0;
                break;
            case SDLK_RIGHT:
                if (v < max) v++;
                break;
            case SDLK_RETURN:
                return 1;
            default: break;
        }
        uistate.keypressed = 0;
        uistate.keyentered = 0;   
    }
    uistate.lastwidget = id;
 

   
    if (uistate.activeitem == id) {   // Update widget value
        mousepos = uistate.mousex - (x + 8);
        if (mousepos < 0) mousepos = 0;
        if (mousepos > 100) mousepos = 100;
        v = (mousepos * max) / 100;
    }

    if (v != value) {
        *pvalue = v;
        return 1;
    }
    else return 0;
}

void drawHeat(unsigned int x, unsigned int y, int onheat)
{
    if(onheat){
        disp_rectangle(x    ,  y    ,x    + 40 , y    + 4  , COLOR_RED);
        disp_rectangle(x-16 ,  y+6  ,x-16 + 72 , y+6  + 10 , COLOR_LGRAY);
        disp_rectangle(x-16 ,  y+10 ,x-16 + 72 , y+10 + 3  , COLOR_GRAY);
        disp_rectangle(x    ,  y+18 ,x    + 40 , y+18 + 4  , COLOR_RED);
    } 
    else {                                              
        disp_rectangle(x    ,  y    ,x    + 40 , y    + 4  , COLOR_DGRAY);
        disp_rectangle(x-16 ,  y+6  ,x-16 + 72 , y+6  + 10 , COLOR_LGRAY);
        disp_rectangle(x-16 ,  y+10 ,x-16 + 72 , y+10 + 3  , COLOR_GRAY);
        disp_rectangle(x    ,  y+18 ,x    + 40 , y+18 + 4  , COLOR_DGRAY);
    }
}

void drawTime(unsigned int x, unsigned int y, unsigned int bcd_time)
{
    unsigned char maskh;
    unsigned char maskl;
    unsigned int show_bcd_time;

    show_bcd_time = bcd_time;
    maskl = show_bcd_time & 0xf;
    maskh = (show_bcd_time  >> 4) & 0xf;

    disp_common16(x +64 +4 ,y,maskl + 1,UI_BG);
    disp_common16(x +56 +4 ,y,maskh + 1,UI_BG);
    disp_common16(x +48 +4 ,y-2,81     ,UI_BG); // :

    show_bcd_time = show_bcd_time >> 8;
    maskl = show_bcd_time & 0xf;
    maskh = (show_bcd_time  >> 4) & 0xf;
    disp_common16(x +40 +4 ,y,maskl + 1,UI_BG);
    disp_common16(x +32 +4 ,y,maskh + 1,UI_BG);
    disp_common16(x +24 +4 ,y-2,81     ,UI_BG); // :
 
    show_bcd_time = show_bcd_time >> 8;
    maskl = show_bcd_time & 0xf;
    maskh = (show_bcd_time  >> 4) & 0xf;
    disp_common16(x +16 +4 ,y,maskl + 1,UI_BG);
    disp_common16(x +8 +4  ,y,maskh + 1,UI_BG);
}

void drawBattery(unsigned int x, unsigned int y, unsigned int full)
{
	const unsigned int len=42;
	const unsigned int hight=18;
	unsigned int startx=x;
	const unsigned int starty=y;
	//const unsigned int len1=4;
	const unsigned int hight1=4;
	int i ,endx;
	disp_rectangle(startx-4,starty+(hight-hight1)/2,startx,starty+(hight-hight1)/2+hight1,COLOR_WHITE);
    disp_rectangle(startx,starty,startx+len,starty+hight,COLOR_WHITE);
	endx = startx + len -2;
    if(full ==0)
	{
        return ;
	}
	if(full ==1)
	{
        disp_rectangle(endx-3,starty+1,endx,starty+hight-1,COLOR_RED);
		return ;
	}
	for(i=0;i<full;i++)
	{
        disp_rectangle(endx-len/5-3,starty+1,endx,starty+hight-1,COLOR_GREEN);
		endx -=len/5;
	}
}

